package com.lujianfei.waterpower.ui.settings;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

import com.lujianfei.waterpower.R;
import com.lujianfei.waterpower.base.BaseActivity;
import com.lujianfei.waterpower.utils.SharePreferenceUtils;
import com.lujianfei.waterpower.utils.ToastUtils;

import androidx.annotation.Nullable;

public class SetttingActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settting);
        setTitleMiddleText("设置");
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    public static class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {

        public static final String key_power_unit = "key_power_unit";
        public static final String key_water_unit = "key_water_unit";
        EditTextPreference etPowerUnit;
        EditTextPreference etWaterUnit;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settingsfragment);
            etPowerUnit = (EditTextPreference) findPreference(key_power_unit);
            etWaterUnit = (EditTextPreference) findPreference(key_water_unit);
        }

        @Override
        public void onResume() {
            super.onResume();
            refreshUI();
            etPowerUnit.setOnPreferenceChangeListener(this);
            etWaterUnit.setOnPreferenceChangeListener(this);
        }

        private void refreshUI() {
            double powerUnit = SharePreferenceUtils.getInstance().getPowerUnit();
            etPowerUnit.setText(""+powerUnit);
            etPowerUnit.setSummary(String.format("%s元/度", powerUnit));
            double waterUnit = SharePreferenceUtils.getInstance().getWaterUnit();
            etWaterUnit.setText(""+waterUnit);
            etWaterUnit.setSummary(String.format("%s元/吨", waterUnit));
        }

        @Override
        public void onPause() {
            super.onPause();
            etPowerUnit.setOnPreferenceChangeListener(null);
            etWaterUnit.setOnPreferenceChangeListener(null);
        }

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            try {
                double powerUnit = Double.parseDouble("" + newValue);
                if (preference.getKey().equalsIgnoreCase(key_power_unit)) {
                    SharePreferenceUtils.getInstance().setPowerUnit(powerUnit);
                } else if (preference.getKey().equalsIgnoreCase(key_water_unit)) {
                    SharePreferenceUtils.getInstance().setWaterUnit(powerUnit);
                }
                refreshUI();
            }catch (Exception e){
                ToastUtils.show(""+e);
            }
            return false;
        }
    }
}
